package com.cvit.controller;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cvit.model.Msg;
import com.cvit.model.Vregion;
import com.cvit.model.Vscience;
import com.cvit.model.Vscience1;
import com.cvit.service.HomePageService;

@Controller
public class HomePageController {

	@Autowired
	HomePageService homePageService;
	
	/**
	 * @author WYHY
	 * @param 首页统计
	 * */
	@RequestMapping("/count")
	@ResponseBody
	public Msg home(){
		List<Vregion> listr=homePageService.regionStatistics();//地区
		List<Vscience> lists=homePageService.scienceStatistics();//理科
		List<Vscience1> lists1=homePageService.science1Statistics();//文科
		return Msg.success().add("vr", listr).add("vs", lists).add("vs1", lists1);
	}
	
	/**
	 * 导出Excel		地区人数统计
	 * @throws Exception 
	 * */
	@RequestMapping("/VregionExport")  
	public @ResponseBody void export(HttpServletRequest request, HttpServletResponse response) throws Exception {  
		String vregionData = "地区人数统计表";
	    if(vregionData!=""){
			response.reset(); //清除buffer缓存  
	        Map<String,Object> map=new HashMap<String,Object>();  
	        response.setContentType("application/octet-stream;charset=utf-8");  
	        response.setHeader("Content-Disposition", "attachment;filename="  
	                + new String(vregionData.getBytes(),"iso-8859-1") + ".xlsx"); 
	        response.setHeader("Pragma", "no-cache");  
	        response.setHeader("Cache-Control", "no-cache");  
	        response.setDateHeader("Expires", 0);   
	        XSSFWorkbook workbook=null;  
	        workbook = homePageService.exportExcelInfo(vregionData);  
	        OutputStream output;  
	        try {
	            output = response.getOutputStream();  
	            BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output);  
	            bufferedOutPut.flush(); 
	            workbook.write(bufferedOutPut);  
	            bufferedOutPut.close();  
	        } catch (IOException e) {  
	            e.printStackTrace();  
	        }  
	    }  
	}
	
	/**
	 * 导出Excel
	 * @throws Exception 	理科人数统计
	 * */
	@RequestMapping("/VscienceExport")  
	public @ResponseBody void VscienceExport(HttpServletRequest request, HttpServletResponse response) throws Exception {  
		String VscienceData = "理科人数统计表";
	    if(VscienceData!=""){
			response.reset(); //清除buffer缓存  
	        Map<String,Object> map=new HashMap<String,Object>();  
	        response.setContentType("application/octet-stream;charset=utf-8");  
	        response.setHeader("Content-Disposition", "attachment;filename="  
	                + new String(VscienceData.getBytes(),"iso-8859-1") + ".xlsx"); 
	        response.setHeader("Pragma", "no-cache");  
	        response.setHeader("Cache-Control", "no-cache");  
	        response.setDateHeader("Expires", 0);   
	        XSSFWorkbook workbook=null;  
	        workbook = homePageService.VscienceExportExcelInfo(VscienceData);  
	        OutputStream output;  
	        try {
	            output = response.getOutputStream();  
	            BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output);  
	            bufferedOutPut.flush(); 
	            workbook.write(bufferedOutPut);  
	            bufferedOutPut.close();  
	        } catch (IOException e) {  
	            e.printStackTrace();  
	        }  
	    }  
	}
	
	/**
	 * 导出Excel
	 * @throws Exception 	文科人数统计
	 * */
	@RequestMapping("/VsciencesExport")  
	public @ResponseBody void VsciencesExport(HttpServletRequest request, HttpServletResponse response) throws Exception {  
		String Vscience1Data = "文科人数统计表";
	    if(Vscience1Data!=""){
			response.reset(); //清除buffer缓存  
	        Map<String,Object> map=new HashMap<String,Object>();  
	        response.setContentType("application/octet-stream;charset=utf-8");  
	        response.setHeader("Content-Disposition", "attachment;filename="  
	                + new String(Vscience1Data.getBytes(),"iso-8859-1") + ".xlsx"); 
	        response.setHeader("Pragma", "no-cache");  
	        response.setHeader("Cache-Control", "no-cache");  
	        response.setDateHeader("Expires", 0);   
	        XSSFWorkbook workbook=null;  
	        workbook = homePageService.VsciencesExportExcelInfo(Vscience1Data);  
	        OutputStream output;  
	        try {
	            output = response.getOutputStream();  
	            BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output);  
	            bufferedOutPut.flush(); 
	            workbook.write(bufferedOutPut);  
	            bufferedOutPut.close();  
	        } catch (IOException e) {  
	            e.printStackTrace();  
	        }  
	    }  
	}
}
